import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {LogicInfo, LogicInfoPage} from './logicAdmin';
import {BaseComponent} from '../common/base.component';
import {LogicAdminService} from './logicAdmin.service';

declare var $: any;

@Component({
  selector: 'app-logicadmin',
  templateUrl: './logicAdmin.component.html'
})
export class LogicAdminComponent extends BaseComponent implements OnInit {
  curPage: Number = 1;
  logicInfoPage: LogicInfoPage;
  logicInfo: LogicInfo[];
  curlogicInfo: LogicInfo;
  logic: [{}];
  path: [{}];
  mapId: string;
  pathId: string;
  fileId: string;

  constructor(private router: Router, private logicAdminService: LogicAdminService) {
    super();
  }

  ngOnInit() {
    this.curlogicInfo = {
      sort: '',
      nodeId: '',
      logicName: '',
      realName: '',
      type: '',
      pathName: '',
      fileUrl: '',
      createTime: '',
      name: '',
      textId: '',
      mapId: '',
      nodeUrlId: '',
      pathId: '',
      fileIds: [],
      text: '',
      realNodeId: ''
    };
    this.searchLogic();
    this.queryListMap();
  }

  // 跳转到逻辑新增
  onAddLogic() {
    this.router.navigate(['/buz/logicAdd']);
  }

  // 查询
  searchLogic() {
    this.curPage = 1;
  }

  // 更新
  onEditLogic(value: LogicInfo) {
    this.curlogicInfo = value;
    this.logicAdminService.getNodeId(this.curlogicInfo);
    this.router.navigate(['/buz/getLogic']);
  }

// 删除
  onDeleteLogic(value: LogicInfo) {
    this.curlogicInfo = value;
    $('#delete_confirm').modal('show'); // 显示编辑对话框
  }

// 删除
  delete() {
    this.logicAdminService.removeLogicInfo(this.curlogicInfo)
      .then(result => {
        if (result) {
          this.queryList();
          this.alert.info('删除成功');
        } else {
          this.alert.error('删除失败');
        }
      });
    $('#delete_confirm').modal('hide');
  }

  onPage(event) {
    this.curPage = event;
    this.queryList();
  }

  // 查询列表
  private queryList() {
    this.logicAdminService.getLogic(this.curPage, this.mapId, this.pathId)
      .then(
        page => {
          this.logicInfoPage = page;
          this.logicInfo = page.logicInfo.datas;
        }
      );
  }

  // 查询Map
  queryListMap() {
    this.logicAdminService.getMap()
      .then(
        res => {
          if (res[0]) {
            this.logic = res[1].mapTO;
          }
        }
      );
  }

  // 查询不同mapId对应不同的路径
   queryListPath(mapId: string) {
    this.mapId = mapId;
    this.pathId = '';
    this.logicAdminService.getPath(this.curPage, this.mapId)
      .then(
        res => {
          if (res[0]) {
            this.path = res[1].datas;
            this.queryList();
          }
        }
      );
  }

  // 选择路径前请选择地图
  selectPath() {
    if (this.mapId === undefined) {
      alert('请先选择地图');
    } else {
    }
  }

  // 获取路径id进行查询传值
  getId(pathId: string) {
    this.pathId = pathId;
    this.queryList();
  }
}
